//
//  LoginVC.swift
//  mmda
//
//  Created by 李陆涛 on 2018/4/23.
//  Copyright © 2018年 mmda. All rights reserved.
//

import UIKit
import RxSwift
import SkyFloatingLabelTextField
import TransitionButton
import IQKeyboardManagerSwift

class LoginVC: BaseTabVC {
    
    @IBOutlet weak var activeLabel: ActiveLabel!
    
    @IBOutlet weak var mobileTF: SkyFloatingLabelTextField!
    
    @IBOutlet weak var codeTF: SkyFloatingLabelTextField!
    
    @IBOutlet weak var getCodeBtn: UIButton!
    @IBOutlet weak var commitBtn: TransitionButton!
    
    @IBOutlet weak var logoTop: NSLayoutConstraint!
    
    var viewModel:LoginViewModel = LoginViewModel()

    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        self.logoTop.constant = 60

        UIView.animate(withDuration: 0.5) {

            [weak self] in
            guard let `self` = self else { return }

            self.view.layoutIfNeeded()
        }
    }
    
    func setupUI() {
        
//        let view = UIView(frame: CGRect(x: 0, y: -kStatusBarHeight, width: kScreenWidth, height: kNavigationBarHeight))
//
//        view.backgroundColor = UIColor.black

        self.navigationController?.navigationBar.setBackgroundImage(UIImage(color: .black), for: UIBarMetrics.default)
        
        let customType = viewModel.aggrementLabelType
        activeLabel.enabledTypes = [customType]
        
        activeLabel.customize { label in
            
            label.configureLinkAttribute = self.viewModel.aggrementLAbelAttr
            
            label.customColor[customType] = .red
            label.customSelectedColor[customType] = .purple
            label.handleCustomTap(for: customType) {
                
                [weak self] element in
                guard let `self` = self else { return }
                
                let webvc = WebVC()
                
                webvc.url = self.viewModel.aggrementURL
                webvc.title = self.viewModel.aggrementTitle
                
                self.navigationController?.pushViewController(webvc, animated: true)
            }
        }
        
        mobileTF.rx.text.orEmpty.bind(to: viewModel.mobile).disposed(by: disposeBag)
        codeTF.rx.text.orEmpty.bind(to: viewModel.code).disposed(by: disposeBag)

        viewModel.mobileValid.bind(to: codeTF.rx.isEnabled).disposed(by: disposeBag)

        viewModel.getCodeValid.bind(to: getCodeBtn.rx.isOKWithTextAndTextColor).disposed(by: disposeBag)
        
        viewModel.everythingValid.bind(to: commitBtn.rx.isOKWithBGColor).disposed(by: disposeBag)
        
        
        viewModel.message.asObservable().bind(to: self.view.rx.showToast).disposed(by: disposeBag)
//        viewModel.showLoading.asObservable().bind(to: self.view.rx.showToastActivity).disposed(by: disposeBag)
        
        viewModel.loginSuccess = {
            
            [weak self] errmsg in
            guard let `self` = self else { return }
            
            
            if let _ = errmsg {
                
                self.commitBtn.stopAnimation(animationStyle: StopAnimationStyle.shake)
                
                return
            }
            
            
            self.commitBtn.stopAnimation(animationStyle: StopAnimationStyle.expand, revertAfterDelay: 0.25, completion: {
                
                self.dismiss(animated: false, completion: nil)
                NotificationCenter.default.post(name: kLoginSuccessNotifationName, object: nil)
            })
        }
        
        let mobile = AppManager.share.mobile ?? ""
        
        self.mobileTF.text = mobile
        self.mobileTF.becomeFirstResponder()
        
    }
    
    @IBAction func closeLogin(_ sender: Any) {
        
        self.dismiss(animated: true, completion: nil)
    }
    
    @IBAction func getCode(_ sender: Any) {
        
        IQKeyboardManager.shared.resignFirstResponder()
        self.viewModel.getCode()
    }
    
    @IBAction func commit(_ sender: Any) {
        
        IQKeyboardManager.shared.resignFirstResponder()
        
        commitBtn.startAnimation()
        self.viewModel.login()
        
    }
}
